#-------------------------------------------------------------------------------
# AMD Lib/Makefile
#-------------------------------------------------------------------------------

LIBRARY = libamd
VERSION = 2.4.6
SO_VERSION = 2

default: library

include ../../SuiteSparse_config/SuiteSparse_config.mk

# AMD depends on SuiteSparse_config
LDLIBS += -lsuitesparseconfig

# compile and install in SuiteSparse/lib
library:
	$(MAKE) install INSTALL=$(SUITESPARSE)

C = $(CC) $(CF) -I../Include -I../../SuiteSparse_config

#-------------------------------------------------------------------------------
# source files
#-------------------------------------------------------------------------------

AMD = amd_aat amd_1 amd_2 amd_dump amd_postorder amd_defaults \
        amd_post_tree  \
	amd_order amd_control amd_info amd_valid amd_preprocess

INC = ../Include/amd.h ../Include/amd_internal.h \
      ../../SuiteSparse_config/SuiteSparse_config.h

#-------------------------------------------------------------------------------
# object files for each version
#-------------------------------------------------------------------------------

AMDI = $(addsuffix .o, $(subst amd_,amd_i_,$(AMD)))
AMDL = $(addsuffix .o, $(subst amd_,amd_l_,$(AMD)))
OBJ = $(AMDI) $(AMDL)

#-------------------------------------------------------------------------------
# compile each int and long routine (with no real/complex version)
#-------------------------------------------------------------------------------

amd_i_%.o: ../Source/amd_%.c $(INC)
	$(C) -DDINT -c $< -o $@

amd_l_%.o: ../Source/amd_%.c $(INC)
	$(C) -DDLONG -c $< -o $@

#-------------------------------------------------------------------------------
# Create the static library (C versions only)
#-------------------------------------------------------------------------------

static: $(AR_TARGET)

$(AR_TARGET): $(OBJ)
	$(ARCHIVE) $@ $^
	- $(RANLIB) $@

#-------------------------------------------------------------------------------
# compile the Fortran versions and the libamdf77.a library (static only)
#-------------------------------------------------------------------------------

fortran: libamdf77.a

AMDF77 = amd.o amdbar.o

amd.o: ../Source/amd.f
	$(F77) $(F77FLAGS) -c ../Source/amd.f -o amd.o

amdbar.o: ../Source/amdbar.f
	$(F77) $(F77FLAGS) -c ../Source/amdbar.f -o amdbar.o

libamdf77.a: $(AMDF77)
	$(ARCHIVE) libamdf77.a $^
	- $(RANLIB) libamdf77.a

#-------------------------------------------------------------------------------
# install (shared C version only)
#-------------------------------------------------------------------------------

# install AMD
install: $(AR_TARGET) $(INSTALL_LIB)/$(SO_TARGET)

$(INSTALL_LIB)/$(SO_TARGET): $(OBJ)
	@mkdir -p $(INSTALL_LIB)
	@mkdir -p $(INSTALL_INCLUDE)
	@mkdir -p $(INSTALL_DOC)
	$(CC) $(SO_OPTS) $^ -o $@ $(LDLIBS)
	( cd $(INSTALL_LIB) ; ln -sf $(SO_TARGET) $(SO_PLAIN) )
	( cd $(INSTALL_LIB) ; ln -sf $(SO_TARGET) $(SO_MAIN) )
	$(CP) ../Include/amd.h $(INSTALL_INCLUDE)
	$(CP) ../Doc/AMD_UserGuide.pdf $(INSTALL_DOC)
	$(CP) ../README.txt $(INSTALL_DOC)/AMD_README.txt
	chmod 755 $(INSTALL_LIB)/$(SO_TARGET)
	chmod 644 $(INSTALL_INCLUDE)/amd.h
	chmod 644 $(INSTALL_DOC)/AMD_UserGuide.pdf
	chmod 644 $(INSTALL_DOC)/AMD_README.txt

# uninstall AMD
uninstall:
	$(RM) $(INSTALL_LIB)/$(SO_TARGET)
	$(RM) $(INSTALL_LIB)/$(SO_PLAIN)
	$(RM) $(INSTALL_LIB)/$(SO_MAIN)
	$(RM) $(INSTALL_INCLUDE)/amd.h
	$(RM) $(INSTALL_DOC)/AMD_UserGuide.pdf
	$(RM) $(INSTALL_DOC)/AMD_README.txt

#-------------------------------------------------------------------------------
# Remove all but the files in the original distribution
#-------------------------------------------------------------------------------

clean:
	- $(RM) -r $(CLEAN)

purge: distclean

distclean: clean
	- $(RM) -r $(PURGE)

